package com.coocaa.aries.core.tool.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.*;

/**
 * @author bijiahao
 * @date : 2019/4/9.
 * @description
 */
public class JsonUtil {
    private static SerializeConfig mapping = new SerializeConfig();

    private static String dateFormat = "yyyy-MM-dd HH:mm:ss";

    static {
        mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
    }

    private JsonUtil() {
    }

    public static <T> List<T> convertToList(String jsonString, Class<T[]> cls) {
        List<T> list = null;
        Object var3 = null;

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            list = Arrays.asList((T[]) mapper.readValue(jsonString, cls));
        } catch (JsonParseException var5) {
            var5.printStackTrace();
        } catch (JsonMappingException var6) {
            var6.printStackTrace();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        return list;
    }

    public static <T> List<T> json2ObjectLists(String jsonString, Class<?> cls) {
        ArrayList list = null;

        try {
            JSONArray jsonArray = JSON.parseArray(jsonString);
            if (jsonArray != null && jsonArray.size() > 0) {
                list = new ArrayList();

                for (int i = 0; i < jsonArray.size(); ++i) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    Object model = JSONObject.toJavaObject(object, cls);
                    list.add(model);
                }
            }
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return list;
    }

    public static Object json2Object(String jsonString, Class<?> classOfT) {
        return JSON.toJavaObject(JSON.parseObject(jsonString), classOfT);
    }

    public static Map<String, String> json2Map(String jsonString) {
        return (Map) JSON.parse(jsonString);
    }

    public static Map<String, Object> json2MapObj(String jsonString) {
        return (Map) JSON.parse(jsonString);
    }

    public static List<Map<String, Object>> json2Maps(String jsonString) {
        return (List) JSON.parse(jsonString);
    }

    public static List<Map<String, Integer>> json2MapsInt(String jsonString) {
        return (List) JSON.parse(jsonString);
    }

    public static String obj2Json(Object obj) {
        return JSON.toJSONString(obj, mapping, new SerializerFeature[0]);
    }
}
